﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;


//游戏数据
public static class GameData
{
    //路径
    static string save_path = Application.persistentDataPath + "/save.xml";
    static string myMusic_path = Application.persistentDataPath + "/MyMusic";
    static string custom_file = myMusic_path + "/custom.txt";
    //C:\Users\lzw\AppData\LocalLow\lzw\掌中钢琴

    public static int layoutIndex = 0;
    public static int keyVibrateIndex = 0;
    public static int sliderVibrateIndex = 0;
    public static int musicIndex = 0;
    public static float musicSpeed = 1;
    public static bool autoReturn = false;
    public static int selectChar = 0;
    public static string myChar = "+-";
    public static int maxNumber = +3;
    public static int minNumber = -3;
    public static int returnNumber = 0;
    public static bool isShowChinese = true;

    public static KeyData[] keyDatas = new KeyData[7];
    public static AudioClip[,] whiteSounds = new AudioClip[7, 7];

    public static void Save()
    {
        XmlDocument xmlDoc = new XmlDocument();
        XmlElement root = xmlDoc.CreateElement("GameData");
        XmlElement layout_Data = xmlDoc.CreateElement("layout_Data");//布局
        XmlElement keyVibrate_Data = xmlDoc.CreateElement("keyVibrate_Data");//琴键震动
        XmlElement sliderVibrate_Data = xmlDoc.CreateElement("sliderVibrate_Data");//滑条震动
        XmlElement customLayout_Data = xmlDoc.CreateElement("customLayout_Data");//自定义布局
        for (int i = 0; i < 7; i++)
        {
            XmlElement key = xmlDoc.CreateElement("key" + i);
            key.SetAttribute("x", keyDatas[i].x.ToString());
            key.SetAttribute("y", keyDatas[i].y.ToString());
            key.SetAttribute("r", keyDatas[i].r.ToString());
            customLayout_Data.AppendChild(key);
        }

        XmlElement selectMusic_Data = xmlDoc.CreateElement("selectMusic_Data");//选择的歌曲
        XmlElement musicSpeed_Data = xmlDoc.CreateElement("musicSpeed_Data");//歌曲速度
        XmlElement autoReturn_Data = xmlDoc.CreateElement("autoReturn_Data");//自动归位
        XmlElement selectChar_Data = xmlDoc.CreateElement("selectChar_Data");//字符
        selectChar_Data.SetAttribute("MyChar", myChar);
        XmlElement slider_Date = xmlDoc.CreateElement("slider_Date");//滑条数据
        slider_Date.SetAttribute("min", minNumber.ToString());
        slider_Date.SetAttribute("max", maxNumber.ToString());
        slider_Date.SetAttribute("return", returnNumber.ToString());
        XmlElement showChinese_Data = xmlDoc.CreateElement("showChinese_Data");//显示中文

        layout_Data.InnerText = layoutIndex.ToString();
        keyVibrate_Data.InnerText = keyVibrateIndex.ToString();
        sliderVibrate_Data.InnerText = sliderVibrateIndex.ToString();
        selectMusic_Data.InnerText = musicIndex.ToString();
        musicSpeed_Data.InnerText = musicSpeed.ToString();
        autoReturn_Data.InnerText = autoReturn.ToString();
        selectChar_Data.InnerText = selectChar.ToString();
        showChinese_Data.InnerText = isShowChinese.ToString();

        xmlDoc.AppendChild(root);
        root.AppendChild(layout_Data);
        root.AppendChild(keyVibrate_Data);
        root.AppendChild(sliderVibrate_Data);
        root.AppendChild(customLayout_Data);
        root.AppendChild(selectMusic_Data);
        root.AppendChild(musicSpeed_Data);
        root.AppendChild(autoReturn_Data);
        root.AppendChild(selectChar_Data);
        root.AppendChild(slider_Date);
        root.AppendChild(showChinese_Data);

        xmlDoc.Save(save_path);
    }
    public static void Read()
    {
        keyDatas[0] = new KeyData(-800, 50, 1);
        keyDatas[1] = new KeyData(-600, 50, 1);
        keyDatas[2] = new KeyData(-400, 50, 1);
        keyDatas[3] = new KeyData(-200, 50, 1);
        keyDatas[4] = new KeyData(0, 50, 1);
        keyDatas[5] = new KeyData(+200, 50, 1);
        keyDatas[6] = new KeyData(+400, 50, 1);

        if (!Directory.Exists(myMusic_path))
        {
            Directory.CreateDirectory(myMusic_path);
        }

        if (!File.Exists(custom_file))
        {
            using (StreamWriter sw = new StreamWriter(custom_file))
            {
                sw.WriteLine("15");//把行结束符写入到文本字符串或流
                sw.WriteLine("祝你生日快乐");
                sw.WriteLine("-5祝-5你-6生-5日  1快-7乐，-5祝-5你-6生-5日  2快1乐，-5祝-5你5生3日  1快-7-6乐，4祝4你3生1日  2快1乐");
                sw.WriteLine("-5 -5 -6 -5   1 -7 | -5 -5 -6 -5   2 1 | -5 -5 5 3   1 -7-6 | 4 4 3 1   2 1");
                sw.Flush();
            }
        }
        else
        {
            try//防止目录出问题
            {               

                string[] strPath = Directory.GetFiles(myMusic_path, "*.txt");

                for (int i = 0; i < strPath.Length; i++)
                {
                    try//防止某个文件出问题
                    {
                        MusicData music = new MusicData();
                        using (StreamReader sr = new StreamReader(strPath[i]))
                        {
                            int index = 0;
                            string line = "";

                            // 从文件读取并显示行，直到文件的末尾 
                            while ((line = sr.ReadLine()) != null)
                            {
                                switch (index)
                                {
                                    case 0:
                                        music.time = float.Parse(line);
                                        break;
                                    case 1:
                                        music.name = line;
                                        break;
                                    case 2:
                                        music.content = line;
                                        break;
                                    case 3:
                                        music.simpleContent = line;
                                        break;
                                    default:
                                        break;
                                }
                                index++;
                            }
                        }
                        Musices.all_music.Add(music);
                    }
                    catch
                    {
                        //处理文件出问题
                    }                   
                }               
            }
            catch
            {
                //处理目录出问题
            }
        }

        //判断安卓路径Application.persistentDataPath该路径下 有没有我们需要的文件，如果没有则创建       
        if (!File.Exists(save_path))
        {
            //File.Create(path);
        }
        else
        {
            try
            {
                //实例化一个XML对象
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(save_path);
                //获取文档中的根节点
                XmlNode root = xmlDoc.GetElementsByTagName("GameData")[0];
                layoutIndex = int.Parse(root["layout_Data"].InnerText);
                keyVibrateIndex = int.Parse(root["keyVibrate_Data"].InnerText);
                sliderVibrateIndex = int.Parse(root["sliderVibrate_Data"].InnerText);
                XmlNode customLayout_Data = root["customLayout_Data"];
                for (int i = 0; i < 7; i++)
                {
                    XmlNode key = customLayout_Data["key" + i];
                    keyDatas[i].x = float.Parse(key.Attributes["x"].Value);
                    keyDatas[i].y = float.Parse(key.Attributes["y"].Value);
                    keyDatas[i].r = float.Parse(key.Attributes["r"].Value);
                }
                musicIndex = int.Parse(root["selectMusic_Data"].InnerText);
                musicSpeed = float.Parse(root["musicSpeed_Data"].InnerText);
                autoReturn = bool.Parse(root["autoReturn_Data"].InnerText);
                selectChar = int.Parse(root["selectChar_Data"].InnerText);
                myChar = root["selectChar_Data"].Attributes["MyChar"].Value;

                XmlNode slider_Date = root["slider_Date"];
                minNumber = int.Parse(slider_Date.Attributes["min"].Value);
                maxNumber = int.Parse(slider_Date.Attributes["max"].Value);
                returnNumber = int.Parse(slider_Date.Attributes["return"].Value);
                isShowChinese = bool.Parse(root["showChinese_Data"].InnerText);
            }
            catch
            {

            }
        }
    }
}


public struct KeyLayoutData
{
    //位置
    public Vector3 position;
    //大小
    public Vector2 size;
    //颜色
    public Color32 color;
    //形状
    public int shape;
}